package com.loyal.mwm.ui

import android.content.Intent
import android.text.TextUtils
import android.view.KeyEvent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import com.base.kit.GsonUtil
import com.loyal.core.helper.ViewHelper
import com.loyal.mwm.R
import com.loyal.mwm.base.BaseActivity
import com.loyal.mwm.beans.AccountBean
import com.loyal.mwm.beans.ObservableAccountBean
import com.loyal.mwm.databinding.ActivityLoginBinding
import com.loyal.mwm.handler.LoginHandler
import com.loyal.mwm.libs.api.LoginViewModel
import com.loyal.mwm.util.ImageUtil
import com.loyal.mwm.util.PreferUtil

class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    private lateinit var registerLauncher: ActivityResultLauncher<Intent>
    private val viewModel: LoginViewModel by viewModels()

    override fun actLayoutRes(): Int {
        return R.layout.activity_login
    }

    override fun afterOnCreate() {
        val bean = PreferUtil.getLoginBean(this)
        binding.observableAccountBean = bean
        binding.click = LoginHandler(this, binding)
        binding.drawable = ImageUtil.getBackground(this)
        registerLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                if (RESULT_OK != it.resultCode) return@registerForActivityResult
                val beanJson = it.data?.getStringExtra("beanJson")
                val accountBean = GsonUtil.json2Bean(beanJson, AccountBean::class.java)
                val account = replaceText(accountBean?.username)
                binding.account.setText(account)
                binding.password.setText("")
            }
        binding.actionLogin.setOnClickListener { login() }
    }

    private fun login() {
        val account = ViewHelper.getInputText(binding?.account)
        val password = ViewHelper.getInputText(binding?.password)
        if (TextUtils.isEmpty(account)) {
            showToast("请输入用户名！")
            return
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码！")
            return
        }
        val accountBean = ObservableAccountBean(account, password)
        val beanJson = accountBean.toString()
        viewModel.login(beanJson)
        //登录页面只有登录
        /*   val subscriber = RxProgressSubscriber<String>(this)
           subscriber.setDialogMessage("登录中...").showProgressDialog(true).setTag(accountBean)
           subscriber.setSubscribeListener(this)
           RxUtil.rxExecute(subscriber.loginByJson(accountBean.toString()), subscriber)*/
    }

    private var lastTime = 0L
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - lastTime <= 2000L) {
                finish()
            } else {
                showToast("再次点击返回键退出")
                lastTime = System.currentTimeMillis()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}